/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;

import java.io.IOException;


public abstract class VoJsonSerializer<T> extends JsonSerializer<T> {


    /**
     * 将枚举类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeEnum(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider)
            throws IOException {
        writer.writeFieldName(StringUtils.toCamelCase(propertyName));
        writer.writeString(String.valueOf(value));
    }

    /**
     * 将字符类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeString(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    /**
     * 将整数类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeInt(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    /**
     * 将小数类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeDecimal(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    /**
     * 将布尔类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeBool(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    /**
     * 将日期类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeDateTime(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    /**
     * 将二进制数组类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeBytes(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider)
            throws IOException {
//        String transValue =Base64Utils  Convert.ToBase64String((byte[])((value instanceof byte[]) ? value : null));
//        writeBaseType(writer, transValue, propertyName, serializerProvider);
    }

    /**
     * 将关联类型数据写入到Json中
     *
     * @param writer             Json写入器
     * @param value              要写入的值
     * @param propertyName       要写入的属性名称
     * @param serializerProvider Json序列化类
     */
    protected final void writeAssociation(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider) throws IOException {
        writeBaseType(writer, value, propertyName, serializerProvider);
    }

    private void writeBaseType(JsonGenerator writer, Object value, String propertyName, SerializerProvider serializerProvider)
            throws IOException {
        writer.writeFieldName(StringUtils.toCamelCase(propertyName));
        serializerProvider.defaultSerializeValue(value, writer);
    }


}
